#pragma once
#include <iostream>
#include <ctime>
#include <fstream>
#include <memory>
#include "./include/cppjieba/MPSegment.hpp"
#include "./include/cppjieba/HMMSegment.hpp"
#include "./include/cppjieba/MixSegment.hpp"
#include "./include/cppjieba/KeywordExtractor.hpp"
#include "./include/cppjieba/limonp/Colors.hpp"
#include "./include/cppjieba/Jieba.hpp"

const char *const DICT_PATH = "./include/cppjieba/dict/jieba.dict.utf8";
const char *const HMM_PATH = "./include/cppjieba/dict/hmm_model.utf8";
const char *const USER_DICT_PATH = "./include/cppjieba/dict/user.dict.utf8";
const char *const IDF_PATH = "./include/cppjieba/dict/idf.utf8";
const char *const STOP_WORD_PATH = "./include/cppjieba/dict/stop_words.utf8";

class Cutting_text
{
    static std::shared_ptr<Cutting_text> Jieba_Cutting_text_ptr;
    cppjieba::Jieba jieba;
    std::unordered_set<std::string> Stop_words;

public:
    Cutting_text()
    :jieba(DICT_PATH,HMM_PATH,USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH)
    {
        // 初始化
        std::ifstream ifs(STOP_WORD_PATH);
        std::string line;
        while (getline(ifs, line))
        {
            Stop_words.insert(line);
        }
    }

    std::vector<std::string> Split_Statement(const std::string &text)
    {
        std::vector<std::string> words;
        jieba.Cut(text, words);
        // 对words去除暂停词

        std::vector<std::string> words_no_stop = Remove_stop_words(words);
        return std::move(words_no_stop);
    }

    static std::shared_ptr<Cutting_text> Get_Jieba_Cutting_text()
    {
        if(Jieba_Cutting_text_ptr == nullptr)
        {
            std::mutex mtx;
            if(Jieba_Cutting_text_ptr == nullptr)
            {
                mtx.lock();
                Jieba_Cutting_text_ptr = std::make_shared<Cutting_text>();
                mtx.unlock();
            }
        }
        return Jieba_Cutting_text_ptr;
    }

private:
    std::vector<std::string> Remove_stop_words(const std::vector<std::string>& nums)
    {
        std::list<std::string> lst(nums.begin() , nums.end());
        auto it = lst.begin();
        while(it != lst.end())
        {
            if(Stop_words.count(*it))
                it = lst.erase(it);
            else it++;
        }

        std::vector<std::string> ret(lst.begin() , lst.end());
        return std::move(ret);
    }
};

std::shared_ptr<Cutting_text> Cutting_text::Jieba_Cutting_text_ptr;